package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.business.OrderInfo;
import com.tbit.uqbike.object.business.PageData;
import com.tbit.uqbike.object.pojo.OrderLog;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.TerControlService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * 终端控制
 *
 * @author Leon
 * 2019年11月11日 下午10:00:01
 */
@Slf4j
@Validated
@RestController
@RequestMapping(value = "/terControl", produces = BaseController.MEDIA_JSON_UTF8)
public class TerControlController extends BaseController {
    @Autowired
    private TerControlService terControlService;

    /**
     * 发送指令,要么带有业务标志，要么带上控制备注
     *
     * @param
     * @param request
     * @return
     */
    @RequestMapping("/sendControl")
    @OperateLog(module = ModuleEnum.TER_CONTROL, methods = MethodEnum.SEND_CONTROL_MSG)
    public JsonResult sendControl(@NotNull(message = "ver.userCode.notnull") String userCode,
                                  @NotNull(message = "ver.controlType.notnull") String controlType,
                                  @NotNull(message = "ver.paramName.notnull") String paramName,
                                  String desc, Integer businessCode, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        OrderInfo orderInfo = terControlService.getOrderInfo(userCode, controlType, paramName, desc, businessCode);
        String serNO = terControlService.sendControl(loginInfo, orderInfo);
        return JsonResult.ok(serNO);
    }

    /**
     * 获取结果
     *
     * @param serNO
     * @param request
     * @return
     */
    @RequestMapping("/getControlRet")
    public JsonResult getControlRet(@NotNull(message = "ver.serNO.notnull") String serNO, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);

        OrderInfo info = terControlService.getOrderRsp(loginInfo, serNO);

        return JsonResult.ok(info);
    }

    @RequestMapping("/getOrderHis")
    public JsonResult getOrderHis(HttpServletRequest request, Integer rowCount, Integer pageNO,
                                  Long startDt, Long endDt, String userCode, String machineNO,
                                  String phone,Integer accountUserId,Integer brandId) {
        LoginInfo loginInfo = getLogin(request);
        // 分页获取
        if (null == rowCount || rowCount <= 0 || rowCount > 1000) {
            rowCount = 20;
        }
        if (null == pageNO || pageNO < 1) {
            pageNO = 1;
        }
        if(Objects.isNull(userCode) && Objects.isNull(machineNO) && StringUtils.isBlank(phone) && Objects.isNull(accountUserId)){
            return JsonResult.fail("请输入设备编号、车辆编号、操作人手机号或选择运维人员，四选一进行搜索", TraceUtil.getTraceIdByLocal());
        }
//        log.info("getOrderHis------------>userCode:{},machineNO:{},phone:{},accountUserId:{}",userCode,machineNO,phone,accountUserId);
        PageData<List<OrderLog>> data = terControlService.queryOrderHis(loginInfo, rowCount, pageNO, startDt, endDt, userCode, machineNO, phone, accountUserId,brandId);
        return JsonResult.ok(data.getData(), data.getTotal());
    }

}